home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 351-375 / 351 / pdc / pdcsrc.lzh / PDC / DBX.c < prev    next >
C/C++ Source or Header  |  1990-04-06  |  16KB  |  560 lines

  1.  
  2. /* PDC Compiler - A Freely Distributable C Compiler for the Amiga
  3.  *                Based upon prior work by Matthew Brandt and Jeff Lydiatt.
  4.  *
  5.  * PDC Compiler release 3.3 Copyright (C) 1989 Paul Petersen and Lionel Hummel.
  6.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  7.  *
  8.  * This code is freely redistributable upon the conditions that this 
  9.  * notice remains intact and that modified versions of this file not be 
  10.  * distributed as part of the PDC Software Distribution without the express
  11.  * consent of the copyright holders.
  12.  *
  13.  *------------------------------------------------------------------
  14.  *
  15.  * $Log:    DBX.c,v $
  16.  * Revision 3.33  90/04/04  02:30:41  lionel
  17.  * *** empty log message ***
  18.  * 
  19.  * Revision 3.32  90/02/03  16:23:14  lionel
  20.  * None
  21.  * 
  22.  *------------------------------------------------------------------
  23.  */
  24.  
  25. /*
  26.  * DBX.c
  27.  * 
  28.  * Output dbx-format symbol table data.
  29.  * 
  30.  * This consists of many symbol table entries, each of them a .stabs assembler
  31.  * pseudo-op with four operands: a "name" which is really a description of
  32.  * one symbol and its type, a "code", which is a symbol defined in stab.h
  33.  * whose name starts with N_, an unused operand always 0, and a "value" which
  34.  * is an address or an offset. The name is enclosed in doublequote
  35.  * characters.
  36.  * 
  37.  * Each function, variable, typedef, and structure tag has a symbol table entry
  38.  * to define it. The beginning and end of each level of name scoping within a
  39.  * function are also marked by special symbol table entries.
  40.  * 
  41.  * The "name" consists of the symbol name, a colon, a kind-of-symbol letter, and
  42.  * a data type number.  The data type number may be followed by "=" and a
  43.  * type definition; normally this will happen the first time the type number
  44.  * is mentioned.  The type definition may refer to other types by number, and
  45.  * those type numbers may be followed by "=" and nested definitions.
  46.  * 
  47.  * This can make the "name" quite long. When a name is more than 80 characters,
  48.  * we split the .stabs pseudo-op into two .stabs pseudo-ops, both sharing the
  49.  * same "code" and "value". The first one is marked as continued with a
  50.  * double-backslash at the end of its "name".
  51.  * 
  52.  * The kind-of-symbol letter distinguished function names from global variables
  53.  * from file-scope variables from parameters from auto variables in memory
  54.  * from typedef names from register variables. See `dbxout_symbol'.
  55.  * 
  56.  * The "code" is mostly redundant with the kind-of-symbol letter that goes in
  57.  * the "name", but not entirely: for symbols located in static storage, the
  58.  * "code" says which segment the address is in, which controls how it is
  59.  * relocated.
  60.  * 
  61.  * The "value" for a symbol in static storage is the core address of the symbol
  62.  * (actually, the assembler label for the symbol).  For a symbol located in a
  63.  * stack slot it is the stack offset; for one in a register, the register
  64.  * number. For a typedef symbol, it is zero.
  65.  * 
  66.  */
  67.  
  68. #ifndef GENERATE_DBX
  69.  
  70. #else
  71.  
  72. #include        <stdio.h>
  73. #include        "C.h"
  74. #include        "Expr.h"
  75. #include        "Gen.h"
  76. #include        "Cglbdec.h"
  77. #include        <stab.h>
  78.  
  79. extern char    *litlate();
  80. extern char    *itoa();
  81.  
  82. extern TYP     *maketype();
  83. extern struct snode *declarestmt();
  84. extern struct snode *linestmt();
  85. extern struct enode *makenode();
  86. extern struct dnode *makednode();
  87.  
  88. struct dbx_table_entry {
  89.     TYP            *tp;
  90.     int             link;
  91.     int             status;
  92. };
  93.  
  94. struct _dbx_table {
  95.     enum e_bt       btype;
  96.     int             size;
  97.     char           *name;
  98.     int             index, relative;
  99.     int             low, high;
  100. };
  101.  
  102. struct _dbx_table dbx_table[10] = {
  103.     {bt_long, 4, "int", 1, 1, -2147483648, 2147483647},
  104.     {bt_char, 1, "char", 2, 2, 0, 127},
  105.     {bt_short, 2, "short", 3, 1, -32768, 32767},
  106.     {bt_uchar, 1, "unsigned_char", 4, 1, 0, 255},
  107.     {bt_ushort, 2, "unsigned_short", 5, 1, 0, 65535},
  108.     {bt_unsigned, 4, "unsigned_int", 6, 1, 0, -1},
  109.     {bt_float, 4, "float", 7, 1, 4, 0},
  110.     {bt_double, 8, "double", 8, 1, 8, 0},
  111.     {bt_void, 4, "void", 9, 9, 0, 0},
  112.     {bt_unknown, 4, "???", 10, 1, 0, 0}
  113. };
  114.  
  115. #define DBX_MAX 2048
  116.  
  117. char            dbx_buffer[DBX_MAX];
  118. int             dbx_num_buffer = 0;
  119.  
  120. #define DBX_ENTRIES     100
  121.  
  122. struct dbx_table_entry dbx_entries[DBX_ENTRIES];
  123. int             dbx_num_entries = 0;
  124.  
  125. int
  126. dbx_match(tp1, tp2)
  127.     TYP            *tp1, *tp2;
  128. {
  129.     if (tp1 == tp2)
  130.         return (TRUE);
  131.     if (tp1 != NULL && tp2 != NULL) {
  132.         if (tp1->type == tp2->type) {
  133.             if (tp1->sname == tp2->sname) {
  134.                 if (tp1->val_flag == tp2->val_flag) {
  135.                     if (tp1->size == tp2->size) {
  136.                         return (dbx_match(tp1->btp, tp2->btp));
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.     }
  142.     return (FALSE);
  143. }
  144.  
  145. int
  146. dbx_lookup(tp)
  147.     TYP            *tp;
  148. {
  149.     int             i;
  150.  
  151.     for (i = 1; i <= dbx_num_entries; i++)
  152.         if (dbx_match(tp, dbx_entries[i].tp))
  153.             return (i);
  154.     return (-1);
  155. }
  156.  
  157. int
  158. dbx_install(tp)
  159.     TYP            *tp;
  160. {
  161.     int             num, siz;
  162.     struct dbx_table_entry *ptr;
  163.  
  164.     if (tp != NULL) {
  165.         num = dbx_lookup(tp);
  166.         if (num == -1) {
  167.             num = dbx_install(tp->btp);
  168.             dbx_num_entries++;
  169.             if (dbx_num_entries >= DBX_ENTRIES) {
  170.                 fprintf( stderr, "DIAG -- dbx_install, No room for types\n" );
  171.                 exit(1);
  172.             }
  173.             if (tp->type == bt_struct || tp->type == bt_union)
  174.                 num = 0;
  175.             dbx_entries[dbx_num_entries].tp = tp;
  176.             dbx_entries[dbx_num_entries].link = num;
  177.             dbx_entries[dbx_num_entries].status = 0;
  178.             num = dbx_num_entries;
  179.         }
  180.         return (num);
  181.     }
  182.     return (-1);
  183. }
  184.  
  185. struct dnode   *
  186. dbx_check(dp, buffer)
  187.     struct dnode   *dp;
  188.     char           *buffer;
  189. {
  190.     struct dnode   *dp2;
  191.  
  192.     dp2 = makednode(dp->tag);   /* Make a new node */
  193.  
  194.     *dp2 = *dp;     /* Copy the old node onto the new */
  195.  
  196.     dp->next = dp2;     /* Make the old node ref the new */
  197.  
  198.     dp->sp = litlate(buffer);   /* Save the old string */
  199.  
  200.     dp2->sp = NULL;     /* No space for new string */
  201.  
  202.     return (dp2);       /* Return the new dbug struct */
  203. }
  204.  
  205. char           *
  206. dbx_addreg(s)
  207.     char           *s;
  208. {
  209.     char           *ptr, *loc;
  210.  
  211.     ptr = dbx_buffer;
  212.     for (loc = s; *loc; loc++, ptr++) {
  213.         *ptr = *loc;
  214.         if (':' == *loc)
  215.             *(++ptr) = 'r';
  216.     }
  217.     *ptr++ = '\0';
  218.     return (litlate(dbx_buffer));
  219. }
  220.  
  221. void
  222. dbx_type(num, buffer)
  223.     int             num;
  224.     char           *buffer;
  225. {
  226.     int             loc, entries;
  227.     struct dbx_table_entry *ptr;
  228.  
  229.     ptr = &dbx_entries[num];
  230.  
  231.     while (*buffer)
  232.         ++buffer;
  233.  
  234.     loc = dbx_entries[num].link;
  235.  
  236.     strcat(buffer, "(0,");
  237.     strcat(buffer, itoa(num));
  238.     strcat(buffer, ")");
  239.  
  240.     if (ptr->status == 0) {
  241.         ptr->status = 1;
  242.         if (loc > 0) {
  243.             strcat(buffer, "=");
  244.             if (ptr->tp->type == bt_pointer) {
  245.                 if (ptr->tp->val_flag == 0)
  246.                     strcat(buffer, "*");
  247.                 else {
  248.                     while (*buffer)
  249.                         ++buffer;
  250.                     entries = (ptr->tp->size / ptr->tp->btp->size) - 1;
  251.                     strcat(buffer, "ar(0,1);0;");
  252.                     strcat(buffer, itoa(entries));
  253.                     strcat(buffer, ";");
  254.                 }
  255.             }
  256.             dbx_type(loc, buffer);
  257.         }
  258.     }
  259. }
  260.  
  261. int
  262. dbx_name(sp, dp)
  263.     SYM            *sp;
  264.     struct dnode   *dp;
  265. {
  266.     char           *buffer, *cptr;
  267.     int             clen, nlen, start, stop, num = 0;
  268.     SYM            *sp1;
  269.  
  270.     buffer = dbx_buffer;
  271.     *buffer = '\0';
  272.     dbx_num_buffer = 0;
  273.  
  274.     dp->ref = NULL;
  275.     dp->tag = N_LSYM;
  276.     dp->label = dp->nest = 0;
  277.  
  278.     switch (sp->storage_class) {
  279.     case sc_static:
  280.         num = dbx_install(sp->tp);
  281.         strcat(buffer, sp->name);
  282.         strcat(buffer, ":S");
  283.         dbx_type(num, buffer);
  284.         dp->tag = N_STSYM;
  285.         dp->ref = makenode(en_labcon, sp->value.i, NULL);
  286.         break;
  287.     case sc_auto:
  288.         num = dbx_install(sp->tp);
  289.         if (sp->storage_type == sc_parameter) {
  290.             strcat(buffer, sp->name);
  291.             strcat(buffer, ":p");
  292.             dbx_type(num, buffer);
  293.             dp->ref = makenode(en_autocon, sp->value.i, NULL);
  294.             dp->tag = N_PSYM;
  295.         }
  296.         else {
  297.             strcat(buffer, sp->name);
  298.             strcat(buffer, ":");
  299.             dbx_type(num, buffer);
  300.             dp->ref = makenode(en_autocon, sp->value.i, NULL);
  301.             dp->tag = N_LSYM;
  302.         }
  303.         break;
  304.     case sc_global:
  305.         num = dbx_install(sp->tp);
  306.         if (sp->tp->type == bt_ifunc) {
  307.             num = dbx_install(sp->tp->btp);
  308.             strcat(buffer, sp->name);
  309.             strcat(buffer, ":F(0,");
  310.             strcat(buffer, itoa(num));
  311.             strcat(buffer, ")");
  312.             dp->tag = N_FUN;
  313.         }
  314.         else {
  315.             strcat(buffer, sp->name);
  316.             strcat(buffer, ":G");
  317.             dbx_type(num, buffer);
  318.             dp->tag = N_STSYM;
  319.         }
  320.         dp->ref = makenode(en_nacon, sp->name, NULL);
  321.         break;
  322.     case sc_type:
  323.         num = dbx_install(sp->tp);
  324.         switch (sp->tp->type) {
  325.         case bt_typedef:
  326.             strcat(buffer, sp->name);
  327.             strcat(buffer, ":t(0,");
  328.             strcat(buffer, itoa(num));
  329.             strcat(buffer, ")");
  330.             dp->ref = makenode(en_icon, sp->value.i, NULL);
  331.             break;
  332.         case bt_union:
  333.             dp->ref = makenode(en_icon, sp->value.i, NULL);
  334.  
  335.             strcat(buffer, sp->name);
  336.             strcat(buffer, ":T(0,");
  337.             strcat(buffer, itoa(num));
  338.             strcat(buffer, ")=u");
  339.             strcat(buffer, itoa(sp->tp->size));
  340.  
  341.             clen = 0;
  342.             nlen = strlen(buffer);
  343.  
  344.             cptr = buffer;
  345.             sp1 = sp->tp->lst.head;
  346.  
  347.             while (sp1 != NULL) {
  348.                 if (nlen > DBX_MAX / 2) {
  349.                     dp = dbx_check(dp, buffer);
  350.                     cptr = buffer = dbx_buffer;
  351.                     *buffer = '\0';
  352.                     nlen = clen = 0;
  353.                 }
  354.  
  355.                 while (*cptr)
  356.                     cptr++;
  357.                 strcat(cptr, sp1->name);
  358.                 strcat(cptr, ":");
  359.                 nlen += strlen(cptr);
  360.  
  361.                 while (*cptr)
  362.                     cptr++;
  363.                 start = sp1->value.i * 8;
  364.                 stop = sp1->tp->size * 8;
  365.                 num = dbx_install(sp1->tp);
  366.                 dbx_type(num, cptr);
  367.                 nlen += strlen(cptr);
  368.  
  369.                 while (*cptr)
  370.                     cptr++;
  371.                 strcat(cptr, ",");
  372.                 strcat(cptr, itoa(start));
  373.                 strcat(cptr, ",");
  374.                 strcat(cptr, itoa(stop));
  375.                 strcat(cptr, ";");
  376.                 nlen += strlen(cptr);
  377.  
  378.                 sp1 = sp1->next;
  379.  
  380.                 if (nlen - clen > 60 && (sp1 != NULL)) {
  381.                     strcat(cptr, "\\\\");
  382.                     nlen += 2;
  383.                     clen = nlen;
  384.                 }
  385.             }
  386.             strcat(cptr, ";");
  387.             break;
  388.         case bt_struct:
  389.             dp->ref = makenode(en_icon, sp->value.i, NULL);
  390.  
  391.             strcat(buffer, sp->name);
  392.             strcat(buffer, ":T(0,");
  393.             strcat(buffer, itoa(num));
  394.             strcat(buffer, ")=s");
  395.             strcat(buffer, itoa(sp->tp->size));
  396.  
  397.             clen = 0;
  398.             nlen = strlen(buffer);
  399.  
  400.             cptr = buffer;
  401.             sp1 = sp->tp->lst.head;
  402.  
  403.             while (sp1 != NULL) {
  404.                 if (nlen > DBX_MAX / 2) {
  405.                     dp = dbx_check(dp, buffer);
  406.                     cptr = buffer = dbx_buffer;
  407.                     *buffer = '\0';
  408.                     nlen = clen = 0;
  409.                 }
  410.  
  411.                 while (*cptr)
  412.                     cptr++;
  413.                 strcat(cptr, sp1->name);
  414.                 strcat(cptr, ":");
  415.                 nlen += strlen(cptr);
  416.  
  417.                 while (*cptr)
  418.                     cptr++;
  419.                 start = sp1->value.i * 8;
  420.                 stop = sp1->tp->size * 8;
  421.                 num = dbx_install(sp1->tp);
  422.                 dbx_type(num, cptr);
  423.                 nlen += strlen(cptr);
  424.  
  425.                 while (*cptr)
  426.                     cptr++;
  427.                 strcat(cptr, ",");
  428.                 strcat(cptr, itoa(start));
  429.                 strcat(cptr, ",");
  430.                 strcat(cptr, itoa(stop));
  431.                 strcat(cptr, ";");
  432.                 nlen += strlen(cptr);
  433.  
  434.                 sp1 = sp1->next;
  435.  
  436.                 if (nlen - clen > 60 && (sp1 != NULL)) {
  437.                     strcat(cptr, "\\\\");
  438.                     nlen += 2;
  439.                     clen = nlen;
  440.                 }
  441.             }
  442.             strcat(cptr, ";");
  443.             break;
  444.         case bt_enum:
  445.             dp->ref = makenode(en_icon, sp->value.i, NULL);
  446.  
  447.             strcat(buffer, sp->name);
  448.             strcat(buffer, ":T(0,");
  449.             strcat(buffer, itoa(num));
  450.             strcat(buffer, ")=e");
  451.  
  452.             clen = 0;
  453.             nlen = strlen(buffer);
  454.             cptr = buffer;
  455.             sp1 = sp->tp->lst.head;
  456.  
  457.             while (sp1 != NULL) {
  458.                 if (nlen > DBX_MAX / 2) {
  459.                     dp = dbx_check(dp, buffer);
  460.                     cptr = buffer = dbx_buffer;
  461.                     *buffer = '\0';
  462.                     nlen = clen = 0;
  463.                 }
  464.                 while (*cptr)
  465.                     cptr++;
  466.                 strcat(cptr, sp1->name);
  467.                 strcat(cptr, ":");
  468.                 strcat(cptr, itoa(sp1->value.i));
  469.                 strcat(cptr, ",");
  470.                 nlen += strlen(cptr);
  471.  
  472.                 sp1 = sp1->next;
  473.  
  474.                 if (nlen - clen > 60 && (sp1 != NULL)) {
  475.                     strcat(cptr, "\\\\");
  476.                     nlen += 2;
  477.                     clen = nlen;
  478.                 }
  479.             }
  480.             strcat(cptr, ";");
  481.             break;
  482.         }
  483.         break;
  484.     case sc_const:
  485.         num = dbx_install(sp->tp);
  486.         strcat(buffer, sp->name);
  487.         strcat(buffer, ":const(0,");
  488.         strcat(buffer, itoa(num));
  489.         strcat(buffer, ")");
  490.         break;
  491.     case sc_label:
  492.     case sc_ulabel:
  493.         dp->ref = makenode(en_nacon, sp->name, NULL);
  494.         num = dbx_install(sp->tp);
  495.         strcat(buffer, sp->name);
  496.         strcat(buffer, ":label(0,");
  497.         strcat(buffer, itoa(num));
  498.         strcat(buffer, ")");
  499.         break;
  500.     case sc_external:
  501.     case sc_member:
  502.     default:
  503.         return (0);
  504.     }
  505.     dp->sp = litlate(buffer);
  506.     return (1);
  507. }
  508.  
  509. void
  510. dbx_ident(sp)
  511.     SYM            *sp;
  512. {
  513.     struct snode   *snp;
  514.  
  515.     if (Options.Debug) {
  516.         snp = declarestmt(sp);
  517.         addauto(snp);
  518.     }
  519. }
  520.  
  521. void
  522. dbx_line()
  523. {
  524.     struct snode   *snp;
  525.  
  526.     if (Options.Debug) {
  527.         snp = linestmt();
  528.         addauto(snp);
  529.     }
  530. }
  531.  
  532. void
  533. dbx_init()
  534. {
  535.     int             i;
  536.     struct _dbx_table *dxp;
  537.     struct dbx_table_entry *ptr;
  538.  
  539.     if (!Options.Debug)
  540.         return;
  541.  
  542.     dbx_num_entries = 0;
  543.  
  544.     for (i = 0; i < 10; i++) {
  545.         dxp = &dbx_table[i];
  546.         fprintf( output, 
  547.                  "\tSTABS\t\"%s:t(0,%d)=r(0%d);%d;%d;\",%d,0,0,0\n",
  548.                  dxp->name, dxp->index, dxp->relative, dxp->low, dxp->high,
  549.                  N_LSYM );
  550.         ptr = &dbx_entries[dxp->index];
  551.  
  552.         ptr->tp = maketype(dxp->btype, dxp->size);
  553.         ptr->link = 0;
  554.         ptr->status = 1;
  555.     }
  556.     dbx_num_entries = 10;
  557. }
  558.  
  559. #endif
  560.